package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.Result;
import com.atguigu.commonutils.vo.CourseWebOrderVO;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.ChapterVO;
import com.atguigu.eduservice.entity.vo.CourseQueryVO;
import com.atguigu.eduservice.entity.vo.CourseWebVO;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 讲师的前台接口
 *
 * @author zr
 */
@RestController
@Api(tags = "课程数据")
@RequestMapping("/edu/service/front/course")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService eduCourseService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "分页课程列表")
    @PostMapping("getCourseFrontList/{page}/{limit}")
    public Result getCourseFrontList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,
            @ApiParam(name = "CourseQueryVO", value = "条件查询封装", required = false)
            @RequestBody(required = false) CourseQueryVO courseQueryVO
    ) {
        Page<EduCourse> coursePage = new Page<>(page, limit);
        Map<String, Object> courseFrontList = eduCourseService.getCourseFrontList(coursePage, courseQueryVO);
        return Result.ok(courseFrontList);
    }

    /**
     * 修改课程详细信息接口
     *
     * @param courseId
     * @return
     */
    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "getCourseFrontInfo/{courseId}")
    public Result getCourseFrontInfo(@PathVariable String courseId, HttpServletRequest request) {
        // 编写sql语句 根据课程Id查询课程详细信息
        CourseWebVO courseWebVO = eduCourseService.getCourseFrontInfo(courseId);
        // 根据课程Id查询章节小节
        List<ChapterVO> chapterVOList = eduChapterService.getChapterVideoByCourseId(courseId);
        // 根据token获取会员Id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return Result.fail().code(20002).message("请进行登录");
        }
        boolean buyCourse = orderClient.isBuyCourse(memberId, courseId);
        Map<Object, Object> map = new HashMap<>();
        map.put("courseWebVO", courseWebVO);
        map.put("chapterVOList", chapterVOList);
        map.put("isBuy", buyCourse);
        return Result.ok(map);
    }

    @ApiOperation("查询课程详细信息")
    @GetMapping("getCourseInfoOrder/{courseId}")
    public CourseWebOrderVO getCourseInfoOrder(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        CourseWebVO courseFrontInfo = eduCourseService.getCourseFrontInfo(courseId);
        CourseWebOrderVO courseWebOrderVO = new CourseWebOrderVO();
        // 把courseFrontInfo里面的值复制给courseWebOrderVO
        BeanUtils.copyProperties(courseFrontInfo, courseWebOrderVO);
        return courseWebOrderVO;
    }
}
